package com.songtech.set.controller;


import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.cmdrecord.controller.ComRecordController;
import com.songtech.login.model.SysPosUser;
import com.songtech.set.modle.TerminalInfo;
import com.songtech.set.service.TermianlInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/termianlinfo/")
public class TerminalInfoController {

	private Logger logger = LoggerFactory.getLogger(TerminalInfoController.class);

	@Autowired
	private TermianlInfoService terminalInfoService;
	
	/**
	 * 根据登录名查询记录的条数
	 * @param terminalInfo
	 * @return
	 */
	@RequestMapping(value = "selectTerminalName")
	@ResponseBody
	public JsonResult selectTerminalName(String terminalName,HttpSession session){
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		String imei=user.getImei();
		//String imei="865205033247320";
		Map<String,Object> map = new HashMap<>();
		try {		
			map.put("status", terminalInfoService.selectTerminalName(terminalName,imei));
			return new JsonResult(map, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
		} catch (Exception e) {
			logger.error("查询登录名个数异常信息:"+e.getMessage(),e);
			return   new JsonResult(map, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
	}
	
	/**
	 * 根据Imei来查询设备信息
	 */
	@RequestMapping(value = "selectTerminalInfo")
	@ResponseBody
	public JsonResult selectTerminalInfo(HttpSession session){
		/**
		 * 从用户信息表里面获取imei
		 */
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		String imei=user.getImei();
		//String imei="865205033247320";	
		TerminalInfo terminalInfo=null;
		try {
			terminalInfo=terminalInfoService.selectTerminalInfo(imei);	
		} catch (SQLException e) {
			logger.error("查询设备信息异常信息:"+e.getMessage(),e);
			return  new  JsonResult(terminalInfo, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return  new  JsonResult(terminalInfo, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	

	/**
	 * 修改设备信息
	 * 
	 * @param imei
	 * @return
	 */
	@RequestMapping(value = "updateTerminalInfo")
	@ResponseBody
	public JsonResult updateTerminalInfo(TerminalInfo terminalInfo,HttpSession session) {
		SysPosUser user = (SysPosUser)session.getAttribute("loginUser");
		String imei=user.getImei();
		//String imei="865205033247320";	
		terminalInfo.setImei(imei);
		//获取前台传过来的字符串
		if(StringUtils.isNotEmpty(terminalInfo.getTerminalCutOffDateStr())){
			try {
				//将这个字符串转换date格式
				terminalInfo.setTerminalCutOffDate(new SimpleDateFormat("yyyy-MM-dd").parse(terminalInfo.getTerminalCutOffDateStr()));
			} catch (ParseException e) {

				logger.error("更新设备信息异常信息:"+e.getMessage(),e);
			}
		}
		boolean   flag=terminalInfoService.judge(terminalInfo);
		if(flag){
			return  new  JsonResult(flag, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
		}
		return  new  JsonResult(flag, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
	}

}